package org.openkoala.koala.codechecker;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.internal.resources.File;
import org.eclipse.core.resources.IMarker;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.texteditor.MarkerUtilities;
import org.openkoala.koala.codechecker.vo.CodeCheckerVO;
/**
 * 
 * @author lingen
 *
 */
public class KoalaResourceChangeListener implements IResourceChangeListener {

	
	@Override
	public void resourceChanged(final IResourceChangeEvent event) {
		try {
			System.out.println(event.getSource());
			event.getDelta().accept(new DeltaPrinter());
		} catch (CoreException e) {
			e.printStackTrace();
		}

	}

}
/**
 * ��ر��ɨ��.
 * @author lingen
 *
 */
class DeltaPrinter implements IResourceDeltaVisitor {
	
	
	
	/**
	 * 
	 * @return true��FALSE.\
	 * @throws CoreException
	 * @param
	 */
	public boolean visit(final IResourceDelta delta) throws CoreException {
		final IResource res = delta.getResource();
		
		switch (delta.getKind()) {
		case IResourceDelta.ADDED:
			System.out.print("Resource ");
			System.out.print(res.getFullPath());
			System.out.println(" was added.");
			break;
		case IResourceDelta.REMOVED:

			break;
		case IResourceDelta.CHANGED:
			 if ((delta.getFlags() & IResourceDelta.CONTENT) != 0) {
				 if (res instanceof File && res.getFileExtension().equals("java")) {
						MyWorkspaceJob job = new MyWorkspaceJob(res);
						job.setRule(res);
						job.schedule();
					}
		      }
			
			break;
		}
		return true; // visit the children
	}
}

/**
 * 
 * @author lingen
 *
 */
class MyWorkspaceJob extends WorkspaceJob {

	/**
	 * 
	 */
	private IResource res;

	/**
	 * 
	 * @param res ��Դ
	 */
	public MyWorkspaceJob(final IResource res) {
		super("Koala Code Checker");
		this.res = res;
	}

	public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
		res.deleteMarkers("KoalaCodeChecker.codeCheckerMarker", true, IResource.DEPTH_INFINITE);

		List<CodeCheckerVO> vos = JavaCodeChecker.checkCode(res
				.getLocationURI().getPath());
		for (final CodeCheckerVO vo : vos) {
			Map<String, Object> attributes = new HashMap<String, Object>();
			attributes.put(IMarker.MESSAGE, vo.getMessage());
			attributes.put(IMarker.LOCATION,
					"line " + vo.getLine());
			attributes.put(IMarker.LINE_NUMBER, vo.getLine());
			MarkerUtilities.createMarker(res, attributes,
					"KoalaCodeChecker.codeCheckerMarker");
		}
		return Status.OK_STATUS;
	}
}
